package com.example.demo.controller;

import com.example.demo.bean.ListBean;
import com.example.demo.bean.SongBean;
import com.example.demo.service.ListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Controller
public class ListController {
    @Autowired
    ListService listService;
    //获取用户歌单
    @RequestMapping(value = "/list/getUserLists",method = RequestMethod.POST)
    public Object getUserLists(HttpServletRequest request){
        int uid = Integer.parseInt(request.getParameter("uid").trim());
        return listService.getUserLists(uid);
    }


    //新建歌单
    @RequestMapping(value = "/list/createList",method = RequestMethod.POST)
    public Object createList(HttpServletRequest request){
        Map<String,Object> res = new HashMap<>();
        if(request.getParameter("uid")==null
                ||request.getParameter("open")==null
                ||request.getParameter("introduce")==null
                ||request.getParameter("listName")==null){
            res.put("msg","空数据错误");
            return  res;
        }
        ListBean listBean = new ListBean();
        listBean.setUid(Integer.parseInt(request.getParameter("uid").trim()));
        listBean.setOpen(Integer.parseInt(request.getParameter("open").trim()));
        listBean.setIntroduce(request.getParameter("introduce").trim());
        listBean.setList_name(request.getParameter("listName").trim());
        listBean.setCreation_time(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date().getTime()));


        if(listService.createList(listBean)){
            if(listService.getList(listBean.getUid(),listBean.getList_name(),listBean.getCreation_time())==null){
                System.out.println("歌单获取失败：可能搜索失败或者创建失败");
                res.put("msg","歌单获取失败：可能搜索失败或者创建失败");
                return res;
            }else {
                listBean.setLid(listService.getList(listBean.getUid(),listBean.getList_name(),listBean.getCreation_time()).getLid());
                System.out.println("一个新的歌单被创建，uid:"+listBean.getUid()+" : lid"+listBean.getLid());
                return listBean;
            }

        }else {
            res.put("msg","歌单创建失败");
        }
        return res;
    }
    //删除歌单
    @RequestMapping(value = "/list/deleteList",method = RequestMethod.POST)
    public Object deleteList(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        if(request.getParameter("uid")==null||request.getParameter("lid")==null){
            result.put("msg","空数据错误");
            return  result;
        }
        int uid = Integer.parseInt(request.getParameter("uid").trim());
        int lid = Integer.parseInt(request.getParameter("lid").trim());
        if(listService.checkList(lid)){
            if(listService.deleteList(lid)){
                System.out.println("用户："+uid+",  删除了歌单："+lid);
                result.put("msg","删除成功");
            }else {
                result.put("msg","删除失败");
            }
        }else {
            result.put("msg","歌单不存在");
        }

        return result;
    }
    //修改歌单信息
    @RequestMapping(value = "/list/changeList",method = RequestMethod.POST)
    public Object changeList(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        if(request.getParameter("lid")==null||request.getParameter("listName")==null
            ||request.getParameter("introduce")==null){
            result.put("msg","空数据错误");
            return  result;
        }
        int lid = Integer.parseInt(request.getParameter("lid").trim());
        String listName = request.getParameter("listName").trim();
        String introduce = request.getParameter("introduce").trim();
        if(listService.changeList(lid,listName,introduce)){
            result.put("msg","修改成功");

        }else {
            result.put("msg","修改失败");
        }
        return result;
    }
    //获得歌单内容
    @RequestMapping(value = "/list/listGetSongs",method = RequestMethod.POST)
    public Object listGetSongs(HttpServletRequest request){
        if(request.getParameter("lid")==null){
            return null;
        }
        int lid = Integer.parseInt(request.getParameter("lid").trim());
        return listService.listGetSongs(lid);
    }
    //添加歌单内容
    @RequestMapping(value = "/list/listAddSong",method = RequestMethod.POST)
    public Object listAddSong(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        if(request.getParameter("lid")==null||request.getParameter("sid")==null){
            result.put("msg","存在空信息");
        }
        int lid = Integer.parseInt(request.getParameter("lid").trim());
        int sid = Integer.parseInt(request.getParameter("sid").trim());

        if(listService.listFindSong(lid,sid)!=null) {
            result.put("msg","歌曲已收藏！");
        }else if(listService.listAddSong(lid,sid)){
            result.put("msg","成功");
        }else {
            result.put("msg","歌曲收藏失败!");
        }
        return result;
    }
    //删除歌单内容
    @RequestMapping(value = "/list/listDelSong",method = RequestMethod.POST)
    public Object listDelSong(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        if(request.getParameter("lid")==null
            ||request.getParameter("sid")==null){
            result.put("msg","空数据");
            return result;
        }
        int lid = Integer.parseInt(request.getParameter("lid").trim());
        int sid = Integer.parseInt(request.getParameter("sid").trim());
        if(listService.listFindSong(lid,sid)==null){
            result.put("msg", "目标歌曲并不在该歌单中。");
        }else if(listService.listDelSong(lid,sid)){
            result.put("msg","成功");
        }else {
            result.put("msg","失败");
        }
        return result;
    }
    //查询歌单内容
    @RequestMapping(value = "/list/listFindSong",method = RequestMethod.POST)
    public Object listFindSong(HttpServletRequest request){
        if(request.getParameter("lid")==null||request.getParameter("sid")==null){
            return null;
        }
        int lid = Integer.parseInt(request.getParameter("lid").trim());
        int sid = Integer.parseInt(request.getParameter("sid").trim());
        Map<String,Object> result = new HashMap<>();
        SongBean songBean = listService.listFindSong(lid,sid);
        if(songBean!=null){
            return songBean;
        }else {
            result.put("msg","失败");
        }
        return result;
    }
}
